package com.thitech.sif.context.web;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import com.thitech.sif.context.parser.IContextParser;
import com.thitech.sif.context.parser.XmlContextParser;
import com.thitech.sif.exception.SifException;
import com.thitech.sif.utils.StringUtils;
import com.thitech.sif.utils.log.LogFactory;
import com.thitech.sif.utils.log.SifLogger;

/**
 * Application Context init tool
 * @author shisheng.lian
 *
 */
public class SifContextLoader extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	private SifLogger logger = LogFactory.getLogger(getClass());
	
	private static final String INIT_PARAM_NAME = "resourceXmlPath";
	private static final String VALIDATE = "validateXml";

	public void init(ServletConfig config) throws ServletException {
		String xmlPath = config.getInitParameter(INIT_PARAM_NAME);
		String validateFlag = config.getInitParameter(VALIDATE);
		logger.debug(INIT_PARAM_NAME + " path value is " + xmlPath);
		
		if (StringUtils.isNull(xmlPath)) {
			throw new SifException("resourceXmlPath init param is null!");
		}
		
		boolean validate = true;
		if (!StringUtils.isNull(validateFlag)) {
			validate = new Boolean(validateFlag);
		}
		logger.debug(VALIDATE + " value is " + validateFlag);
		
		String[] xmlPathArr = xmlPath.split(",");
		for (int i = 0; i < xmlPathArr.length; i++) {
			IContextParser parser = new XmlContextParser(xmlPathArr[i], validate);
			parser.parser();
		}
	}

}
